package com.reremouse.rerebbs.web.mb.mag;

import com.reremouse.model.AdminInfo;
import com.reremouse.rerebbs.dao.IAdminDAO;
import com.reremouse.util.JsfHelper;
import com.reremouse.util.SpringHelper;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@ManagedBean
@ViewScoped
public class ListAdmin
        implements Serializable {
    IAdminDAO adminDAO = (IAdminDAO) SpringHelper.getSpringBean("AdminDAO");
    List<AdminInfo> userList = new ArrayList();
    AdminInfo user = ((AdminSessionBean) JsfHelper.getBean("adminSessionBean")).getAdmin();

    public AdminInfo getUser() {
        return this.user;
    }

    public void setUser(AdminInfo user) {
        this.user = user;
    }

    public List<AdminInfo> getUserList() {
        return this.userList;
    }

    public void setUserList(List<AdminInfo> userList) {
        this.userList = userList;
    }

    public ListAdmin() {
        if (this.user != null) {
            if (this.user.getGrp().equals("user")) {
                this.userList.clear();
                this.userList.add(this.adminDAO.findAdmin(this.user.getId()));
            }
            if (this.user.getGrp().equals("admin")) {
                this.userList.clear();
                this.userList = this.adminDAO.findAllAdmin();
            }
        }
    }

    public String delete(long id) {
        AdminInfo u = this.adminDAO.findAdmin(id);
        if (this.user.getUrn().equals(u.getUrn())) {
            FacesContext fc = FacesContext.getCurrentInstance();
            FacesMessage fm = new FacesMessage();
            fm.setSeverity(FacesMessage.SEVERITY_ERROR);
            fm.setSummary("超级管理员不能删除自身！");
            fc.addMessage("", fm);
            return null;
        }
        AdminInfo at = null;
        List<AdminInfo> as = this.adminDAO.findAllAdmin();
        for (AdminInfo ai : as) {
            if ((ai.getGrp().equals("admin")) && (ai.getId() != id)) {
                at = ai;
                break;
            }
        }
        System.out.println(at.getId() + ":" + at.getNickname());
        if (at != null) {
        }
        this.adminDAO.deleteAdmin(id);

        resumeDB();

        return "ListUser";
    }

    void resumeDB() {
        this.userList = this.adminDAO.findAllAdmin();
    }
}



/* Location:           F:\jsp源码\蝙蝠论坛系统\rerebbs\WEB-INF\classes\

 * Qualified Name:     com.reremouse.rerebbs.web.mb.mag.ListAdmin

 * JD-Core Version:    0.7.0.1

 */